0

example.com URL

この URL を呼び出していますが、jsonCallback({"ERROR": "6"});. これが何なのか理解できません。エラーを何度も検索しましたが、役立つものは何も見つかりませんでした。

これは、この URL を呼び出すことで、価格表を取得する Web サイトです。ウェブサイト自体からこれを呼び出すと、データを適切に取得できます。しかし、http リクエストをコピーして URL に貼り付けようとすると、エラーが表示されます。

以下は私のCURLリクエストです:

<?php

   class CURL {
   var $callback = false;

function setCallback($func_name) {
    $this->callback = $func_name;
}

function doRequest($method, $url, $vars) {
    $headers=array();
    $headerVar=0;
    $headers[$headerVar]='Content-Type: text/javascript; charset=UTF-8';    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);       
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
    if ($method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    }
    $data = curl_exec($ch);
    curl_close($ch);

    echo '<br><br>'.curl_error().'-----'.curl_error();
    return $data;

    if ($data) {
        if ($this->callback)
        {
            $callback = $this->callback;
            $this->callback = false;
            return call_user_func($callback, $data);
        } else {
            return $data;
        }
    } else {
        return curl_error($ch);
    }
}

function get($url) {
    return $this->doRequest('GET', $url, 'NULL');
}

function post($url, $vars) {
    return $this->doRequest('POST', $url, $vars);
}
  }


      function stringParameter($array)  {
    $post_items=array();
    foreach ( $array as $key => $value) {
        $post_items[] = $key . '=' . $value;
    }
    //create the final string to be posted using implode()
    $post_string = implode ('&', $post_items);
    return $post_string;
      }

     function build_http_query( $query ){

    $query_array = array();

    foreach( $query as $key => $key_value ){


        if($key_value != ''){
            $query_array[] = urlencode($key) . '=' . urlencode( $key_value );
        }else {
            $query_array[] = urlencode($key) . '=' .  $key_value ;
        }
    }

    return htmlspecialchars(implode( '&', $query_array ));

     }

        list($usec, $sec) = explode(" ", microtime()); 
      $time13 = sprintf('%d%03d', $sec, $usec/1000); 

       $data    =   
    array(
        "brand" => "PERODUA",
        "model" => "VIVA 1.0",
        "gender" => "Male",
        "md_age" => '35',
        "marital_status" => "Married",
        "car_age" => '8',
        "ncd" => '0',
        "data" => 
                        array(
                        "car_data" => array( 
                                                           "make"=>"PERODUA", 
                                        "model"=>"VIVA 1.0", 
                                        "year_of_manufacture"=>"2004", 
                                        "offpeak"=>"yes"    
                                    ),
                        "drivers_data" => array( 
                                        "driver_1"=>    array(
                                                "gender"                => "Male",
                                                "marital_status"        => "Married",
                                                "date_of_birth"         => "22/3/1978",
                                                "year_driving_license"  => "1999",
                                                "ncd"                   => "0",
                                                "occupation"            => "ZADM: Indoor Office/Exec/Admin Staff",
                                                "relationship"          => " "
                                        )       
                                    ),
                        "discount_data" => array(
                                        "certificate_of_merit" => false
                                    ),
                        "claims_data" => array(
                                        "have_claims"   => "no",
                                        "claims_number" => "0",
                                        "claims_amount" => "0"
                                    ),
                        "product_data" =>  array(
                                        "plan"  => null,
                                        "price" => null,
                                        "policy_start"=> "1/5/2013",
                                        "policy_end"=> "30/4/2014",
                                            "ncd"=> false,
                                             "excess"=> null
                                    )                       
                        ) 
        );              

   $encode = build_http_query($data);
$url = 'https://example.com/price?callback=jsonCallback&'.$encode;  
$obj = new CURL();
echo $ppp =  $obj->get($url);


 ?>

jsonで書かれているため、URLのデータを送信するのは間違っていますか?

4

2 に答える 2

0

代わりに使用してみてbuild_http_queryくださいhttp_build_query

于 2013-05-18T09:02:52.517 に答える