2016-08-29 7 views
0
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"firstname":"Mike","lastname":"Doel","customer_id":"12345","email":"[email protected]"}' -u API-key: APIURL(http://) 

obigen Aussage ist im Befehl ausgeführt wird, aber ich bin nicht in der Lage, das gleiche von PHP-Code achive unten ist mein CodePHP cURL, POST JSON Fehler immer in php curl

$url="https://apiurl"; 

$data=array("firstname"=>"Mike","lastname"=>"Doel","customer_id"=>"12345","email"=>"[email protected]"); 

$data_json=json_encode($data); 

//Curl code 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"Accept: application/json","api-key")); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($ch); 
curl_close($ch); 
+0

haben Sie eine Benutzer-ID oder ein Passwort für API-Schlüssel übergeben? –

+0

Bitte beachten Sie http://StackOverflow.com/Questions/3062324/what-is-curl-in-PHP –

Antwort

0
In CURL, for api key you need to pass username:password , if you going to access in php code 

$url="https://apiurl"; 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 

curl_setopt_array($curl, array(
     CURLOPT_URL => $url, 
     CURLOPT_HTTPAUTH => CURLAUTH_BASIC, 
     CURLOPT_USERPWD => 'username:password', 
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_ENCODING => "", 
     CURLOPT_MAXREDIRS => 10, 
     CURLOPT_TIMEOUT => 300000, 
     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
     CURLOPT_CUSTOMREQUEST => 'POST', 
     CURLOPT_HTTPHEADER => array(
      "accept: application/json", 
      "content-type: application/json" 
     ), 
    CURLOPT_POSTFIELDS => $postfields, 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 

curl_close($curl); 

return $response; 

Probieren Sie es einmal ... hoffentlich wird es für Sie arbeiten.

+0

Vielen Dank seine Arbeit .. :) –

0

Diese:

curl_setopt($ch, CURLOPT_HTTPHEADER, array([..snip..], "api-key")); 
                  ^^^^^ 

und

curl [..snip..] -u api-key 
       ^^^^^^^^^^ 

sind NICHT entspricht. -u gibt die HTTP-Standardauthentifizierung mit username:password an. Ihre setopt stopft nur, dass Benutzernamen als der Name eines HTTP-Header, die nicht ist, wie grundlegende auth Anmeldeinformationen angezeigt

Sie

statt
curl_setopt($ch, CURLOPT_USERNAME, "api-key"); 

haben sollte.